package com.runen.wnhz.runen.ui.activity.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.ReceivingAddressEntity;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerAddressComponent;
import com.runen.wnhz.runen.di.module.AddressModule;
import com.runen.wnhz.runen.presenter.Contart.AddressContart;
import com.runen.wnhz.runen.presenter.iPresenter.IAddressPersenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018-04-11.
 */

public class AddAddressActivity extends BaseActivity<IAddressPersenter> implements AddressContart.View,View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    
    @BindView(R.id.rl_select_address)
    RelativeLayout rl_select_address;

    @BindView(R.id.tv_select_address)
    TextView tv_select_address;

    @BindView(R.id.tv_save_address)
    TextView tv_save_address;
    
    @BindView(R.id.sw_default_address)
    Switch sw_default_address;

    @BindView(R.id.et_name)   //收货人地址
    EditText et_name;

    @BindView(R.id.et_phone)   //电话
    EditText et_phone;

    @BindView(R.id.et_address)   //详细地址
     EditText et_address;

    private UserBean reqeustUser;  //用户保存的信息

    private BottomDialog mJdDialog; //京东地址选择器

    private String mTitleText;
    private String isDefault = "1";
    private int mType;
    private String mAid;
    private String mProvince; //省
    private String mCity; //市
    private String mCounty; //区

    @Override
    protected int getLayoutView() {
        return R.layout.add_address_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                break;
            default:
                break;
        }
    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this,R.color.title_green);
        reqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);
        initOnClick();
    }

    private void initOnClick() {
        tv_save_address.setOnClickListener(this);
        rl_select_address.setOnClickListener(this);
        sw_default_address.setOnCheckedChangeListener(this);
    }


    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        initBundle();
        mTitleBuilder.setMiddleTitleText(mTitleText)
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }

    private void initBundle() {
        Bundle bundle = getIntent().getExtras();
        mType = bundle.getInt("type");
        if (mType == 2){
            mTitleText = "修改收货地址";
            ReceivingAddressEntity data = (ReceivingAddressEntity) bundle.getSerializable("data");
            initData(data);
        } else {
            mTitleText = "新增收货地址";
        }
    }

    /**
     * 修改地址 初始化信息
     * @param data
     */
    private void initData(ReceivingAddressEntity data) {
        this.mAid = data.getAid();
        this.isDefault = data.getMoren();
        this.mProvince = data.getProvince();
        this.mCity = data.getCity();
        this.mCounty = data.getCounty();
        et_name.setText(data.getConsignee());
        et_phone.setText(data.getTelephone());
        tv_select_address.setText(data.getProvince() + " " + data.getCity() + " " + data.getCounty());
        et_address.setText(data.getPlace());
        sw_default_address.setChecked(TextUtils.equals(data.getMoren(), "2"));
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerAddressComponent.builder().applicationComponent(appComponent)
                .addressModule(new AddressModule(this))
                .build().initJect(this);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                isDefault = "2";
            } else {
                isDefault = "1";
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_save_address:
                //新增用户
                mPresenter.setAddress();
                break;
            case R.id.rl_select_address:
                if (mJdDialog != null) {
                    mJdDialog.show();
                } else {
                    mJdDialog = new BottomDialog(this);
                    mJdDialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                        @Override
                        public void onAddressSelected(Province province, City city, County county, Street street) {
                            mProvince = province.name;
                            mCity = city.name;
                            mCounty = county.name;

                            tv_select_address.setText(mProvince + " " + mCity + " " + mCounty);

                            if (mJdDialog != null) {
                                mJdDialog.dismiss();
                            }
                        }
                    });
                    mJdDialog.setTextSize(14);//设置字体的大小
                    mJdDialog.setIndicatorBackgroundColor(R.color.text_green);//设置指示器的颜色
                    mJdDialog.setTextSelectedColor(R.color.text_green);//设置字体获得焦点的颜色
                    mJdDialog.setTextUnSelectedColor(R.color.text_black);//设置字体没有获得焦点的颜色
//            dialog.setDisplaySelectorArea("31",1,"2704",1,"2711",0,"15582",1);//设置已选中的地区
                    mJdDialog.show();
                }
                break;
        }
    }

    @Override
    public void showLoadding() {
        
    }

    @Override
    public void dimissLoding() {

    }

    @Override
    public void showErrorMessage(String msg) {

    }

    @Override
    public String getToken() {
        return reqeustUser.getToken();
    }

    @Override
    public String getPage() {
        return null;
    }

    @Override
    public void getReceivingRecycler(List<ReceivingAddressEntity> mlist) {

    }

    @Override
    public String getType() {
        return mType + "";
    }

    @Override
    public String getAid() {
        return mAid;
    }

    @Override
    public String getName() {
        return et_name.getText().toString().trim();
    }

    @Override
    public String getPhone() {
        return et_phone.getText().toString().trim();
    }

    @Override
    public String getprovince() {
        return mProvince;
    }

    @Override
    public String getCity() {
        return mCity;
    }

    @Override
    public String getCounty() {
        return mCounty;
    }

    @Override
    public String getAddress() {
        return et_address.getText().toString().trim();
    }

    @Override
    public String getIsDefault() {
        return isDefault;
    }

    @Override
    public void onSuccess(String info) {
        ToastUtil.showToast(info);
        this.finish();
    }
}
